package org.force4spring;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.axis.session.Session;
import org.force4spring.support.ForceSession;
import org.springframework.transaction.support.ResourceHolderSupport;
import org.springframework.util.Assert;


/**
 * Session holder, wrapping an Apex Session.
 * ApexTransactionManager binds instances of this class
 * to the thread, for a given SforceServiceLocator.
 *
 * <p>Note: This is an SPI class, not intended to be used by applications.
 *
 * @author Juergen Hoeller
 * @author Max Rudman
 * @since 09.01.2007
 * @see ForceTransactionManager
 * @see SessionFactoryUtils
 */
public class SessionHolder extends ResourceHolderSupport {

	private static final Object DEFAULT_KEY = new Object();

	/**
	 * This Map needs to be synchronized because there might be multi-threaded
	 * access in the case of JTA with remote transaction propagation.
	 */
	private final Map sessionMap = Collections.synchronizedMap(new HashMap(1));
	
	// Flag to keep track if "transaction" has been started by ForceTransactionManager.doBegin
	private boolean transactionStarted;

	public SessionHolder(ForceSession session) {
		addSession(session);
	}

	public SessionHolder(Object key, ForceSession session) {
		addSession(key, session);
	}


	public ForceSession getSession() {
		return getSession(DEFAULT_KEY);
	}

	public ForceSession getSession(Object key) {
		return (ForceSession) this.sessionMap.get(key);
	}

	public ForceSession getValidatedSession() {
		return getValidatedSession(DEFAULT_KEY);
	}

	public ForceSession getValidatedSession(Object key) {
		ForceSession session = (ForceSession) this.sessionMap.get(key);
		// Check for dangling Session that's around but already closed.
		// Effectively an assertion: that should never happen in practice.
		// We'll seamlessly remove the Session here, to not let it cause
		// any side effects.
		if (session != null && !session.isOpen()) {
			this.sessionMap.remove(key);
			session = null;
		}
		return session;
	}

	public Session getAnySession() {
		synchronized (this.sessionMap) {
			if (!this.sessionMap.isEmpty()) {
				return (Session) this.sessionMap.values().iterator().next();
			}
			return null;
		}
	}

	public void addSession(ForceSession session) {
		addSession(DEFAULT_KEY, session);
	}

	public void addSession(Object key, ForceSession session) {
		Assert.notNull(key, "Key must not be null");
		Assert.notNull(session, "Session must not be null");
		this.sessionMap.put(key, session);
	}

	public Session removeSession(Object key) {
		return (Session) this.sessionMap.remove(key);
	}

	public boolean containsSession(ForceSession session) {
		return this.sessionMap.containsValue(session);
	}

	public boolean isEmpty() {
		return this.sessionMap.isEmpty();
	}

	public boolean doesNotHoldNonDefaultSession() {
		synchronized (this.sessionMap) {
			return this.sessionMap.isEmpty() ||
					(this.sessionMap.size() == 1 && this.sessionMap.containsKey(DEFAULT_KEY));
		}
	}
	
	public void startTransaction() {
		transactionStarted = true;
	}
	
	public boolean isTransactionStarted() {
		return transactionStarted;
	}
	
	public void clear() {
		super.clear();
		this.transactionStarted = false;
	}
}
